trigger UpdateOpportunityPrimaryOwner on Opportunity(before insert, before update) {
  List<Opportunity> NewOpty= new List<Opportunity>();
  List<id> OptyOwnerIDs= new List<id>();
  ID localAdvertiserRecordTypeID = RecordTypeHelper.GetRecordTypeID('Opportunity', 'SFA-OOR Lead Order');
  ID localAdvertiserRecordType1ID = RecordTypeHelper.GetRecordTypeID('Opportunity', 'SFA-OOR Renewals');
  Map<id,id> NewMap = new Map<id,id>();
  Map<id,id> OldMap = new Map<id,id>();
  List<User> OptyOwners = new list<User>();
  Map<id,User> OwnerMap = new Map<id,User>();
  Set<String> excptionProfileList = new Set<String>{'SFA OOR Finance Analysts'};


  if(trigger.isInsert){
    for(Opportunity opp :Trigger.new){
        if(opp.RecordTypeId == localAdvertiserRecordTypeID || opp.RecordTypeId ==localAdvertiserRecordType1ID){
            NewOpty.add(opp);            
            OptyOwnerIDs.add(opp.ownerid);
        }
    }
    if(OptyOwnerIDs.size()>0)
    OptyOwners = [select id,Name,AFrontier_Employee_ID__c,ATTUID__c from user where id in :OptyOwnerIDs];
    for(User U : OptyOwners ){
         OwnerMap.put(U.Id,U);
     }

    for(Opportunity opty : NewOpty){
        opty.Primary_Owner__C =  OwnerMap.get(opty.ownerid).Name; 
        opty.Primary_Owner_AFrontier_Employee_ID__c=OwnerMap.get(opty.ownerid).AFrontier_Employee_ID__c;
        opty.Primary_Owner_ATTUID__c=OwnerMap.get(opty.ownerid).ATTUID__c;
        }
    }
    
    if(Trigger.IsUpdate){
            for(Opportunity opid :Trigger.New){
                if((opid.RecordTypeId == localAdvertiserRecordTypeID || opid.RecordTypeId ==localAdvertiserRecordType1ID)&& opid.OwnerID!=Trigger.OldMap.get(opid.Id).OwnerID){
                    NewMap.put(opid.id, opid.ownerid);
                    NewOpty.add(opid);
                    OptyOwnerIDs.add(opid.ownerid);
                 }
            }

            for(Opportunity opid :Trigger.old){
                if(opid .RecordTypeId == localAdvertiserRecordTypeID){
                    OldMap.put(opid.id, opid.ownerid);}
                }
            
             if(OptyOwnerIDs.size()>0)  
             OptyOwners = [select id,AFrontier_Employee_ID__c,ATTUID__c,Name
                           from user where id IN:OptyOwnerIDs AND Profile.Name NOT IN :excptionProfileList];
              for(User U : OptyOwners ){
                  OwnerMap.put(U.Id, U);
              }
             for(opportunity opp: NewOpty){
             	 // prevent primary owner field gets updated to any OOR finance users
                if(OwnerMap.containskey(NewMap.get(opp.id)) && NewMap.get(opp.id) != OldMap.get(opp.id)){
                opp.Primary_Owner__C =  OwnerMap.get(opp.ownerid).Name; 
                opp.Primary_Owner_AFrontier_Employee_ID__c=OwnerMap.get(opp.ownerid).AFrontier_Employee_ID__c;
                opp.Primary_Owner_ATTUID__c=OwnerMap.get(opp.ownerid).ATTUID__c;

                 }
                 //System.Debug('UPDATEDOWNER'+opp.Primary_Owner__C);

                }                
          }

    
    

    
    
}